package com.example.teachingSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teachingSystem.pojo.entity.SysUserRole;
import com.example.teachingSystem.pojo.form.user.UserRoleForm;
import com.example.teachingSystem.pojo.vo.OptionVo;
import com.example.teachingSystem.service.SysUserRoleService;
import com.example.teachingSystem.mapper.SysUserRoleMapper;
import com.example.teachingSystem.utils.result.Result;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 86147
 * @description 针对表【sys_user_role(用户和角色关联表)】的数据库操作Service实现
 * @createDate 2023-12-03 12:23:26
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole>
        implements SysUserRoleService {

    @Override
    public Result<String> addUserRoles(UserRoleForm userRoleForm) {
        List<SysUserRole> userRoleList = new ArrayList<>();
        this.remove(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, userRoleForm.getUserId()));
        for (Long roleId : userRoleForm.getRoleIds()) {
            SysUserRole one = new SysUserRole();
            one.setUserId(userRoleForm.getUserId());
            one.setRoleId(roleId);
            userRoleList.add(one);
        }
        this.saveBatch(userRoleList);
        return Result.ok(null, "修改成功");
    }

    @Override
    public Result<String> deleteUserRoles(UserRoleForm userRoleForm) {
        this.remove(new LambdaQueryWrapper<SysUserRole>()
                .eq(SysUserRole::getUserId, userRoleForm.getUserId())
                .eq(SysUserRole::getRoleId,userRoleForm.getRoleIds().get(0)));
        return Result.ok(null, "删除成功");
    }
}




